#ifndef __AIRB_VIDEO_H__
#define __AIRB_VIDEO_H__

#define FREEGLUT_STATIC
#include <gl/freeglut.h>

namespace AIRB
{


class Video
{
public:

	//LazyPeople
	void Setup(int argc, char* argv[],void(*callback)(void),char* title=NULL);

	//Window Function
	void windowSetMetrics(int width,int height,float scale,int x=0,int y=0);
	void windowCreate(int argc, char* argv[],char *title=NULL);

	//OpenGL
	void glInit();
	void gl2d();

	//Instance
	static Video* CreateInstance(){Video::singleton_instance = new Video;return Video::singleton_instance;}
	static Video* InstanceSafe(){ return ((Video::singleton_instance)? Video::singleton_instance : Video::CreateInstance() );}
	static Video* Instance(){ return singleton_instance;}
	static Video* Inst(){ return singleton_instance;}


	int window_width;
	int window_height;
	int window_hwidth;
	int window_hheight;
	int window_pos_x;
	int window_pos_y;
	float window_view_scale;
	float window_view_x;
	float window_view_y;

protected:
	Video();

	static Video* singleton_instance;
};

void Video::Setup(int argc, char* argv[],void(*callback)(void),char* title)
{
	windowCreate(argc,argv,title);
	glInit();
	gl2d();

	glutDisplayFunc(callback);
	glutIdleFunc(callback);

	//Keyboard
	//glutKeyboardFunc(processNormalKeys);

	//adding here the mouse processing callbacks
	//glutMouseFunc(processMouse);
	//glutMouseWheelFunc(processMouseWheel);
	//glutMotionFunc(processMouseActiveMotion);
	//glutPassiveMotionFunc(processMousePassiveMotion);
	//glutEntryFunc(processMouseEntry);

	glutMainLoop();
}

void Video::glInit()
{
	//glEnable(GL_TEXTURE_2D);									// Enable Texture Mapping ( NEW )
	glShadeModel(GL_SMOOTH);									// Enable Smooth Shading
	glClearColor(1.0f, 1.0f, 1.0f, 0.5f);						// Black Background
	glClearDepth(100.0f);											// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);									// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);										// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations
}

void Video::gl2d()
{
	int scale = 2;		//not really scaling, but dividing the width/height into equal parts to center viewport in 2d
	int vPort[4];		//Simply used to hold gl viewport info

   glGetIntegerv(GL_VIEWPORT, vPort);

   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();

   //glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
   glOrtho(-(vPort[2]/scale), (vPort[2]/scale), -(vPort[3]/scale), (vPort[3]/scale), -1000, 1000);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();
   
 
  return;
}

void Video::windowSetMetrics(int width,int height,float scale,int x,int y)
{
	window_width = width;
	window_height = height;
	window_hwidth = window_width/2;
	window_hheight = window_height/2;
	window_pos_x = x;
	window_pos_y = y;
	window_view_scale = scale;
}

void Video::windowCreate(int argc, char* argv[],char *title)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowPosition(window_pos_x,window_pos_y);
	glutInitWindowSize(window_width,window_height);
	glutCreateWindow((title)?title:"AirBash Framework");
}



Video::Video()
{
	windowSetMetrics(800,600,0,0);
	window_pos_x = 0;
	window_pos_y = 0;
	window_view_x = 0;
	window_view_y = 0;
}


}
#endif
